package com.soma.game.service.impl;

import com.soma.game.entity.Game;
import com.soma.game.entity.GameStatic;
import com.soma.game.entity.GameStaticExample;
import com.soma.game.entity.User;
import com.soma.game.enums.BooleanStatus;
import com.soma.game.enums.GameStaticType;
import com.soma.game.mapper.GameStaticExtMapper;
import com.soma.game.mapper.GameStaticMapper;
import com.soma.game.model.RankUserVo;
import com.soma.game.service.GameService;
import com.soma.game.service.GameStaticService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GameStaticServiceImpl extends BaseServiceImpl<GameStaticMapper, GameStatic, GameStaticExample> implements GameStaticService {


    @Resource
    private GameStaticExtMapper gameStaticExtMapper;

    @Resource
    private GameService gameService;

    @Override
    public GameStatic getGameStatic(User user, Game game) {
        GameStaticExample example = new GameStaticExample();
        example.createCriteria().andActiveFlgEqualTo(BooleanStatus.Y).andUserIdEqualTo(user.getId()).andGameIdEqualTo(game.getId());
        List<GameStatic> gameStatics = selectByExample(example);
        if(CollectionUtils.isEmpty(gameStatics)){
            GameStatic gameStatic = new GameStatic();
            gameStatic.setType(game.getStaticType());
            gameStatic.setGameId(game.getId());
            gameStatic.setUserId(user.getId());
            gameStatic.setUid(user.getUid());
            gameStatic.setRecordScore(0l);
            save(gameStatic);
            return gameStatic;
        } else {
            return gameStatics.stream().findFirst().orElse(null);
        }
    }

    @Override
    public List<RankUserVo> getRankUserVos(Long gameId, List<Long> uidList, Long uid) {

        String uids = StringUtils.join(uidList, "','");

        return gameStaticExtMapper.getRankUserVos(gameId, "'" + uids + "'", uid);
    }

    @Override
    public void clearScore(GameStaticType gameStaticType) {
        List<Game> games = gameService.getListByStaticType(gameStaticType);

        GameStatic gameStatic = new GameStatic();
        gameStatic.setRecordScore(0L);

        games.stream().forEach(game -> {
            GameStaticExample example = new GameStaticExample();
            example.createCriteria().andGameIdEqualTo(game.getId());
            updateByExampleSelective(gameStatic, example);
        });
    }

    @Override
    public void clearScore(Long uid, Long gameId) {
        GameStaticExample example = new GameStaticExample();
        example.createCriteria().andActiveFlgEqualTo(BooleanStatus.Y).andUidEqualTo(uid).andGameIdEqualTo(gameId);
        List<GameStatic> gameStatics = selectByExample(example);
        if(!CollectionUtils.isEmpty(gameStatics)){
            GameStatic gameStatic = gameStatics.get(0);
            gameStatic.setRecordScore(0l);
            save(gameStatic);
        }

    }
}